package com.rjet.view_demo1;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

public class DemoAdapter extends BaseQuickAdapter<DemoEntity, BaseViewHolder> {

    //EditText编辑回调
    interface DemoEditTextListener{

        void finish(String content, Integer index);
    }

    private DemoEditTextListener demoEditTextListener;

    public void setDemoEditTextListener(DemoEditTextListener demoEditTextListener) {
        this.demoEditTextListener = demoEditTextListener;
    }

    public DemoAdapter(int layoutResId, @Nullable List<DemoEntity> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder holder, DemoEntity item) {

        Log.d(TAG, "convert: ");
        
        //关闭复用
        //holder.setIsRecyclable(false);

        TextView name = holder.getView(R.id.name);
        TextView age = holder.getView(R.id.age);

        name.setText(item.getName());
        age.setText(item.getAge()+"");


        EditText content = holder.getView(R.id.conent);

        //先移除监听
        if (content.getTag() instanceof TextWatcher) {
            content.removeTextChangedListener((TextWatcher)content.getTag());
            content.clearFocus();
        }
        //content.setText("");//移除后设置数据
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.d(TAG, "afterTextChanged: " + content.getText());
                if (content.hasFocus()) {
                    //写你editText的数据改变逻辑
                    //notifyDataSetChanged();
                }
            }
        };
        //重新添加
        content.addTextChangedListener(watcher);
        //设置tag
        content.setTag(watcher);

    }
}
